package com.sinszm.common;

import org.springframework.context.ApplicationContext;

/**
 * 实例读取器
 *
 * @author chenjianbo
 */
public final class SpringContext {

    private ApplicationContext applicationContext = null;

    private static class Spring {
        private static SpringContext holder = new SpringContext();
    }

    private SpringContext() {
    }

    public static SpringContext instance() {
        return Spring.holder;
    }

    void init(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    private void check() {
        assert this.applicationContext != null;
    }

    public ApplicationContext context() {
        check();
        return this.applicationContext;
    }

    public <T> T getBean(Class<T> clazz){
        check();
        return context().getBean(clazz);
    }

    public Object getBean(String name){
        check();
        return context().getBean(name);
    }

    public <T> T getBean(String name,Class<T> clazz){
        check();
        return context().getBean(name, clazz);
    }

    public String activeProfile() {
        return context().getEnvironment().getActiveProfiles()[0];
    }

}